
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package meeple;

//~--- JDK imports ------------------------------------------------------------

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.net.*;

import java.util.Enumeration;

/**
 *
 * @author rogier
 */
import java.util.Hashtable;
import java.util.Hashtable;
import java.util.Stack;

import javax.imageio.*;

public class TileData {
    static private Hashtable<String, Tile> tile_data = null;

    public TileData() {
        if (tile_data == null) {
            tile_data = new Hashtable<String, Tile>();
            tile_data.put("CRRR", new Tile("CRRR", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.road, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.road, TileType.field,
                TileType.junction
            }, 3, "CRRR50.jpg"));
            tile_data.put("CRRCS", new Tile("CRRCS", new TileType[] {
                TileType.shield, TileType.city, TileType.city, TileType.field, TileType.road, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.city
            }, 2, "CRRCS50.jpg"));
            tile_data.put("FRRR", new Tile("FRRR", new TileType[] {
                TileType.field, TileType.field, TileType.field, TileType.field, TileType.road, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.road, TileType.field,
                TileType.junction
            }, 4, "FRRR50.jpg"));
            tile_data.put("CCRCS", new Tile("CCRCS", new TileType[] {
                TileType.shield, TileType.city, TileType.city, TileType.city, TileType.city, TileType.city,
                TileType.field, TileType.road, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.city
            }, 2, "CCRCS50.jpg"));
            tile_data.put("CRFR", new Tile("CRFR", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.road, TileType.field,
                TileType.field, TileType.field, TileType.field, TileType.field, TileType.road, TileType.field,
                TileType.road
            }, 4, "CRFR50.jpg"));
            tile_data.put("CCFCS", new Tile("CCFCS", new TileType[] {
                TileType.shield, TileType.city, TileType.city, TileType.city, TileType.city, TileType.city,
                TileType.field, TileType.field, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.city
            }, 1, "CCFCS50.jpg"));
            tile_data.put("CRRF", new Tile("CRRF", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.road, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.field
            }, 3, "CRRF50.jpg"));
            tile_data.put("FFRR", new Tile("FFRR", new TileType[] {
                TileType.field, TileType.field, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.road, TileType.field,
                TileType.road
            }, 9, "FFRR50.jpg"));
            tile_data.put("CFFC", new Tile("CFFC", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.field, TileType.field,
                TileType.field, TileType.field, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.city
            }, 3, "CFFC50.jpg"));
            tile_data.put("CFRR", new Tile("CFRR", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.field, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.road, TileType.field,
                TileType.road
            }, 3, "CFRR50.jpg"));
            tile_data.put("FCFCS", new Tile("FCFCS", new TileType[] {
                TileType.field, TileType.field, TileType.field, TileType.shield, TileType.city, TileType.city,
                TileType.field, TileType.field, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.city
            }, 2, "FCFCS50.jpg"));
            tile_data.put("FFFFL", new Tile("FFFFL", new TileType[] {
                TileType.field, TileType.field, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.field, TileType.field, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.cloister
            }, 4, "FFFFL50.jpg"));
            tile_data.put("CCFF", new Tile("CCFF", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.city, TileType.city, TileType.city,
                TileType.field, TileType.field, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.field
            }, 2, "CCFF50.jpg"));
            tile_data.put("CCRC", new Tile("CCRC", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.city, TileType.city, TileType.city,
                TileType.field, TileType.road, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.city
            }, 1, "CCRC50.jpg"));
            tile_data.put("CFFCS", new Tile("CFFCS", new TileType[] {
                TileType.shield, TileType.city, TileType.city, TileType.field, TileType.field, TileType.field,
                TileType.field, TileType.field, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.city
            }, 2, "CFFCS50.jpg"));
            tile_data.put("FCFC", new Tile("FCFC", new TileType[] {
                TileType.field, TileType.field, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.field, TileType.field, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.field
            }, 1, "FCFC50.jpg"));
            tile_data.put("RFRF", new Tile("RFRF", new TileType[] {
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.road
            }, 8, "RFRF50.jpg"));
            tile_data.put("FFRFL", new Tile("FFRFL", new TileType[] {
                TileType.field, TileType.field, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.cloister
            }, 2, "FFRFL50.jpg"));
            tile_data.put("CCCCS", new Tile("CCCCS", new TileType[] {
                TileType.shield, TileType.city, TileType.city, TileType.city, TileType.city, TileType.city,
                TileType.city, TileType.city, TileType.city, TileType.city, TileType.city, TileType.city, TileType.city
            }, 1, "CCCCS50.jpg"));
            tile_data.put("CRRC", new Tile("CRRC", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.road, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.road
            }, 3, "CRRC50.jpg"));
            tile_data.put("CFCF", new Tile("CFCF", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.field, TileType.field,
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.field, TileType.field,
                TileType.field
            }, 3, "CFCF50.jpg"));
            tile_data.put("RRRR", new Tile("RRRR", new TileType[] {
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.road, TileType.field,
                TileType.field, TileType.road, TileType.field, TileType.field, TileType.road, TileType.field,
                TileType.junction
            }, 1, "RRRR50.jpg"));
            tile_data.put("CCFC", new Tile("CCFC", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.city, TileType.city, TileType.city,
                TileType.field, TileType.field, TileType.field, TileType.city, TileType.city, TileType.city,
                TileType.city
            }, 3, "CCFC50.jpg"));
            tile_data.put("CFFF", new Tile("CFFF", new TileType[] {
                TileType.city, TileType.city, TileType.city, TileType.field, TileType.field, TileType.field,
                TileType.field, TileType.field, TileType.field, TileType.field, TileType.field, TileType.field,
                TileType.field
            }, 5, "CFFF50.jpg"));
        }
    }

    Tile getTile(String name) {
        return tile_data.get(name);
    }

    Enumeration<String> getNames() {
        return tile_data.keys();
    }

    Stack<Tile> createStack() {
        Stack<Tile>         stack = new Stack<Tile>();
        Enumeration<String> names = getNames();

        while (names.hasMoreElements()) {
            Tile tile = getTile(names.nextElement());

            for (int i = 0; i < tile.getNumber(); i++) {
                stack.add(new Tile(tile));
            }
        }

        shuffle(stack);

        return stack;
    }

    Hashtable<String, BufferedImage> createImages() {
        BufferedImage                    img;
        Hashtable<String, BufferedImage> images = new Hashtable<String, BufferedImage>();
        Enumeration<Tile>                tiles  = tile_data.elements();

        try {

            // / URL url = this.getClass().getResource("/images/black.jpg");
            // img = ImageIO.read(url);
            // images.put("DEFAULT", img);
            while (tiles.hasMoreElements()) {
                Tile tile = tiles.nextElement();
                URL  url  = this.getClass().getResource("/images/" + tile.getName());

                img = ImageIO.read(url);
                images.put(tile.getName(), img);
            }
        } catch (IOException e) {
            System.out.println("Could not load image");
        }

        return images;
    }

    private void shuffle(Stack stack) {

        /*
         * Uses the Fisher_Yates shuffle algorithm
         */
        Object temp;
        int    tempIndex;

        for (int i = stack.size() - 1; i >= 0; i--) {
            tempIndex = ((int) (i * Math.random()));

            if (tempIndex != i) {    // swap if different elements
                temp = stack.get(i);
                stack.set(i, stack.get(tempIndex));
                stack.set(tempIndex, temp);
            }
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
